package com.changgou.seckill.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.seckill.config.TokenDecode;
import com.changgou.seckill.service.SeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/seckillorder")
public class SeckillOrderController {

    @Autowired
    private TokenDecode tokenDecode;

    @Autowired
    private SeckillOrderService seckillOrderService;

    @RequestMapping("/add")
    public Result add(@RequestParam("time") String time,@RequestParam("id") Long id){
        //动态获取登录人
        String username = tokenDecode.getUserInfo().get("username");
        //业务层秒杀下单
        boolean result = seckillOrderService.add(id, time, username);
        //返回结果
        if (result){
            //成功
            return  new Result(true, StatusCode.OK,"抢购成功");
        }else{
            //失败
            return  new Result(false, StatusCode.ERROR,"抢购失败");
        }
    }
}
